﻿(function (mainModule) {
    "use strict";
    
mainModule.directive('sortHeader', function () {
    return {
        restrict: 'EA',
        require: 'defaultSort',
        scope: {            
            defaultSort: "=",
            sortAction: '&'
        },
        controller: ["$scope", function ($scope) {
            this.sorting = { column: $scope.defaultSort.column, ascending: $scope.defaultSort.ascending };

            this.sortBy = function (column) {
                if (this.sorting.column === column) {
                    this.sorting.ascending = !this.sorting.ascending;
                } else {
                    this.sorting.column = column;
                    this.sorting.ascending = true;
                }

                $scope.sortAction({ sorting: this.sorting });
            }
        }]
    }
});
mainModule.directive('sortColumn', function () {
    return {
        restrict: 'EA',
        require: "^sortHeader",
        scope: {        
            header: '@',
            column: '@sortColumn'              
        },
        template:            
            '<a href="#" ng-click="sortBy(column)">' +
            '<span ng-bind="header"></span> ' +
            '<span ng-show="sorting.column === column">' +
                '<span class="glyphicon glyphicon-chevron-up" ng-show="!sorting.ascending"></span>' +
                '<span class="glyphicon glyphicon-chevron-down" ng-show="sorting.ascending"></span>' +
            '</span>' +
            '</a>',        
        link: function ($scope, element, attrs, parentCtrl) {
            $scope.sorting = parentCtrl.sorting;
            $scope.sortBy = parentCtrl.sortBy;
        }        
    }
});

})(mainModule);